package com.itheima.nacos.consumer.controller;

import com.itheima.microservice.service1.api.Service1Api;
import com.itheima.microservice.service2.api.Service2Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @author WangYongHao
 * @version 1.0
 */
@RestController
public class RestConsumerController {
    @Value("${provider.address}")
    private String providerAddress;

    @GetMapping({"/service"})
    public String service() {
        RestTemplate restTemplate = new RestTemplate();
        String providerResult = (String) restTemplate.getForObject("http://" + this.providerAddress + "/service", String.class, new Object[0]);
        return "consumer invoke | " + providerResult;
    }

    // 使用nacos完成服务调用
    // 服务Id即注册中心中的服务名
    private String serviceId = "nacos-restful-provider";

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping(value = "/service1")
    public String service1() {
        RestTemplate restTemplate = new RestTemplate();
        //调用服务
//        String providerResult = restTemplate.getForObject("http://" + providerAddress + "/service", String.class);
        ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
        URI uri = serviceInstance.getUri();
        String providerResult = restTemplate.getForObject(uri + "/service", String.class);
        return "consumer invoke | " + providerResult;
    }

    // 实现dubbo远程调用
    @org.apache.dubbo.config.annotation.Reference
    private Service2Api service2Api;

    @GetMapping(value = "/service2")
    public String service2() {
        // 远程调用service2
        String providerResult = service2Api.dubboService2();
        return "consumer dubbo invoke | " + providerResult;
    }

    // 实现微服务之间dubbo的远程调用
    @org.apache.dubbo.config.annotation.Reference
    private Service1Api service1Api;

    @GetMapping(value = "/service3")
    public String service3() {
        String providerResult = service1Api.dubboService1();
        return "consumer dubbo invoke | " + providerResult;
    }

    @Value("${common.name}")
    private String common_name;
    // 注入配置文件上下文
    @Autowired
    private ConfigurableApplicationContext applicationContext;
    @GetMapping("/configs")
    public String getValue() {
//        return common_name;
//        return applicationContext.getEnvironment().getProperty("common.name");
        String name = applicationContext.getEnvironment().getProperty("common.name");
        String addr = applicationContext.getEnvironment().getProperty("common.addr");
        return name + "|" + addr;
    }
}
